#include <time.h>
#include <iostream>
#include <string>
#include <list>
#include "ConsoleMediator.h"
#include "PlayersFactory.h"


int RandomInt(int from , int to) {              // return random integer between [from-to] 
	int rnd = from + rand()%(to-from+1);
	return rnd;
}

using namespace std;

int  main()
{	

	enum {
		Sony = 0,
		Samsung = 1
	};
	enum {
		DVD = 0,
		VCR = 1,
		CD = 2
	};

	srand(time(0));
	int medType = RandomInt(Sony,Samsung);
	if (medType == Sony)
	{
		cout << "Creating a Console of type Sony " << endl;
	}
	else 
	{
		cout << "Creating a Console of type Samsung " << endl;
	}


	ConsoleMediator* mediator = new ConsoleMediator();
	PlayersFactory* playerFactory;
	if (medType == Sony)
	{
		playerFactory = new SonyPlayerFactory(mediator);
	}
	else 
	{
		playerFactory = new SamsungPlayerFactory(mediator);
	}
	list<Player*> all_players;
	int playerCount = RandomInt(5,15);
	for (int i=0;i<playerCount;i++) 
	{
		int playerType = RandomInt(0,2);
		Player* tempPlayer = 0;
		if (playerType == DVD) 
		{
			tempPlayer = playerFactory->DVDPlayerDevice();
			mediator->addDVDPlayer((DVDPlayer*) tempPlayer);
		}
		if (playerType == VCR)
		{
			tempPlayer = playerFactory->VCRPlayerDevice();
			mediator->addVCRPlayer((VCRPlayer*) tempPlayer);
		}
		else 
		{
			tempPlayer = playerFactory->CDPlayerDevice();
			mediator->addCDPlayer((CDPlayer*) tempPlayer);
		}
		all_players.push_back(tempPlayer);
	}

	for (int i=0;i<500;i++)
	{
		int currentPlayer = RandomInt(0,playerCount-1);
		list<Player*>::iterator iter = all_players.begin();
		for ( ;currentPlayer>0 && iter != all_players.end(); iter++)
		{
			currentPlayer--;
		}

		if (RandomInt(0,1)==0)
		{
			(*iter)->start();
		}
		else 
		{
			(*iter)->stop();
		}


	}


	delete playerFactory;
	delete mediator;
	return 0;
}